import {_decorator, Button, Component, instantiate, Label, Prefab} from 'cc';
import UCard from './UCard';
import {ClickEventType, OrderCardStatus} from '../GameDef';
import {BaseCard, CardInOrder} from '../models/Card';

const {ccclass, property} = _decorator;


@ccclass('Lattice')
export default class Lattice extends Component{
    @property(Label)
    orderType: Label

    @property(Prefab)
    cardPrefab: Prefab = null;


    get isAdded() {
        return this.node.children.length > 0;
    }

    init(card:CardInOrder,name:string){
        if(card.status == OrderCardStatus.FINISH) {
            this.addCard(card)
        }
        this.orderType.string = name;
    }

    addCard(cardData:BaseCard){
        let cardNode = instantiate(this.cardPrefab)
        let card = cardNode.getComponent(UCard);
        card.init(0,1,ClickEventType.ORDER);
        card.isOpen = true;
        card.refresh(cardData)
        card.getComponent(Button).interactable = false;
        this.node.addChild(cardNode);
    }
}